﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Entity.Models;
using IServices;
using Models;
using Prism.Commands;
using Prism.Services.Dialogs;

namespace IntelligentParkingLot.ViewModels.Pages.Dialogs
{
    public class EditMenuDialogViewModel : IDialogAware
    {
        private readonly IMenuService _menuService;

        public EditMenuDialogViewModel(IMenuService menuService)
        {
            _menuService = menuService;

            SaveCommand = new DelegateCommand(DoSave);
        }

        public event Action<IDialogResult> RequestClose;

        public MenuItemModel MenuModel { get; set; } = new MenuItemModel();
        public List<SysMenu> ParentNodes { get; set; } = new List<SysMenu>();
        public DelegateCommand SaveCommand { get; set; }
        public string Title { get; set; } = "编辑菜单项";

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed() { }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            var model = parameters.GetValue<MenuItemModel>("model");
            if (model == null)
            {
                Title = "新增菜单项";

                MenuModel.ParentId = 0; // 默认新增一个一级菜单
                MenuModel.MenuType = 1; // 默认新增一个集合类型的节点
            }
            else
            {
                Title = "编辑菜单项";
                var sm = _menuService.GetMenuById(model.Id);
                MenuModel.Id = sm.Id;
                MenuModel.MenuHeader = sm.MenuHeader;
                MenuModel.ParentId = sm.ParentId.Value;
                MenuModel.TargetView = sm.TargetView;
                MenuModel.MenuIcon = sm.MenuIcon;
                MenuModel.MenuType = sm.MenuType.Value;
            }

            ParentNodes = _menuService.GetMenuList(m => m.MenuType == 1).ToList();
            ParentNodes.Insert(0, new SysMenu { MenuHeader = "ROOT", Id = 0 });
        }

        private void DoSave()
        {
            try
            {
                if (MenuModel.Id == 0)
                {
                    // 新增
                    _menuService.Add(
                        new SysMenu
                        {
                            MenuHeader = MenuModel.MenuHeader,
                            MenuIcon = MenuModel.MenuIcon,
                            TargetView = MenuModel.TargetView,
                            ParentId = MenuModel.ParentId,
                            MenuType = MenuModel.MenuType,
                            State = 1
                        }
                    );
                }
                else
                {
                    // 编辑
                    var entity = _menuService.GetMenuById(MenuModel.Id);
                    entity.MenuHeader = MenuModel.MenuHeader;
                    entity.MenuIcon = MenuModel.MenuIcon;
                    entity.TargetView = MenuModel.TargetView;
                    entity.ParentId = MenuModel.ParentId;
                    entity.MenuType = MenuModel.MenuType;
                    _menuService.Update(entity);
                }
                RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
